<?php

namespace app\admin\controller;

use think\Db;
use think\Session;
use think\Validate;
use app\service\FileService;

class File extends Common
{
    public function index()
    {
        $type = input('type/d') ?: 1;
    
        $where = ['group_id' => $type, 'is_delete'=>0];

        $this->assign('type', $type);


        $search_name = input('param.search_name');
        if ($search_name) {
            $where['file_name'] = ['like', '%' . $search_name . '%'];
        }
        $order = input('order') ? input('order') : 'desc';
        if ($order == 1) {
            $order = 'desc';
        } else if ($order == 2) {
            $order = 'asc';
        }

        $start = input('start');
        $end = input('end');
        $this->assign('start', $start);
        $this->assign('end', $end);
        if ($start && $end) {
            $where['create_time'] = ['between', [strtotime($start), strtotime($end)]];
        } else if ($start && !$end) {
            $where['create_time'] = ['egt', strtotime($start)];
        } else if (!$start && $end) {
            $where['create_time'] = ['elt', strtotime($end)];
        }
        

        $data = Db::name('file_system')
            ->field('*')
            ->where($where)
            ->order('file_id ' . $order)
            ->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $page = $data->render();
        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }
    public function down()
    {
        $id = input('id/d');
        $file = Db::name('file_system')->where(['file_id'=>$id])->find();
        if($file){
            return ['ret'=>'http://' . $file['domain'] . $file['file_path']];
        }else{
            return ['err'=>'文件不存在'];
        }
    }

    public function del()
    {
        $id = input('id/d');
        $file = Db::name('file_system')->where(['file_id'=>$id, 'is_delete'=>0])->find();
        if(!$file){
            return ['err'=>'文件不存在'];
        }
        $res = Db::name('file_system')->where(['file_id'=>$id])->update(['is_delete'=>1, 'delete_time'=>time()]);
        if($res){
            return ['ret'=>'删除成功'];
        }else{
            return ['err'=>'删除失败'];
        }
    }
    public function clearRecov()
    {
        $files = Db::name('file_system')->where(['is_delete'=>1])->select();
        if(sizeof($files) == 0){
            return ['ret'=>'已清空'];
        }
        $rmids = [];
        foreach ($files as $key => $value) {
            $fileinsystempath = ROOT_PATH . $value['file_path'];
            if(file_exists($fileinsystempath)){
                try {
                    @unlink($fileinsystempath);
                    $rmids[] = $value['file_id'];
                } catch (\Exception $e) {
                    
                }
            }else{
                $rmids[] = $value['file_id'];
            }
        }
        $res = Db::name('file_system')->where(['file_id'=>['in', $rmids]])->delete();
        if($res){
            return ['ret'=>'已从回收站彻底删除' . sizeof($rmids) . '个文件'];
        }else{
            return ['err'=>'删除失败'];
        }
    }



    public function add()
    {
        $id = input("id");
        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) { //添加数据

                $db = FileService::add($post);

                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else { //修改数据

                // $db = FileService::edit($post);

                // if ($db) {
                //     $data = ['ret' => '修改成功'];
                // } else {
                //     $data = ['err' => '修改失败'];
                // }
            }
        } else {
            if ($id) {
                $name = Db::name('file_system')->where('file_id', $id)->find();

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }
}